package com.example.itshome.activity;

import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.example.itshome.R;
import com.example.itshome.fragment.HelpPageFragment;
import com.example.itshome.fragment.MainPageFragment;
import com.example.itshome.fragment.MyPageFragment;
import com.example.itshome.manager.UserInfoManager;
import com.example.itshome.view.BottomRailView;

public class MainPageActivity extends AppCompatActivity implements BottomRailView.OnTabCheckListener {



    private BottomRailView mBottomRailView;
    private Fragment[] mFragments;
    private int currentFragmentPosition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        initFindView();
        initFragment();
        initBottomRailView();
        UserInfoManager.getInstance().initUser(this);
    }

    void initFindView(){
        mBottomRailView=findViewById(R.id.brv_main_page);

    }
    
    void initFragment(){
        mFragments=new Fragment[3];
        mFragments[0]=new HelpPageFragment();
        mFragments[1]=new MainPageFragment();
        mFragments[2]=new MyPageFragment();
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction transaction=fm.beginTransaction();
        transaction.add(R.id.fl_activity_main_page,mFragments[0]);
        transaction.add(R.id.fl_activity_main_page,mFragments[1]);
        transaction.add(R.id.fl_activity_main_page,mFragments[2]);
        transaction.show(mFragments[1]);
        transaction.hide(mFragments[0]);
        transaction.hide(mFragments[2]);
        transaction.commit();
        currentFragmentPosition=1;
    }


    void initBottomRailView(){
        BottomRailView.Tab tab1=new BottomRailView.Tab()
                .setmIconNormalResId(R.drawable.help_normal)
                .setmIconPressedResId(R.drawable.help_press)
                .setText("救助");
        mBottomRailView.addTab(tab1);
        BottomRailView.Tab tab2=new BottomRailView.Tab()
                .setmIconNormalResId(R.drawable.main_normal)
                .setmIconPressedResId(R.drawable.main_press)
                .setText("主页");
        mBottomRailView.addTab(tab2);
        BottomRailView.Tab tab3=new BottomRailView.Tab()
                .setmIconNormalResId(R.drawable.my_normal)
                .setmIconPressedResId(R.drawable.my_press)
                .setText("我的");
        mBottomRailView.addTab(tab3);
        mBottomRailView.updatePosition(1);
        mBottomRailView.setOnTabCheckListener(this);

    }


    @Override
    public void onTabSelected(View v, int position) {
        changeFragmentTo(position);
    }

    private void changeFragmentTo(int position){
        if(position>=0&&position<3&&position!=currentFragmentPosition){
            FragmentManager fm=getSupportFragmentManager();
            FragmentTransaction transaction=fm.beginTransaction();
            if(position>currentFragmentPosition){
                transaction.setCustomAnimations(R.anim.main_page_fragment_change_right_in_anima,R.anim.main_page_fragment_change_left_out_anima);
            }else {
                transaction.setCustomAnimations(R.anim.main_page_fragment_change_left_in_anima,R.anim.main_page_fragment_change_right_out_anima);
            }
            transaction.show(mFragments[position]);
            transaction.hide(mFragments[currentFragmentPosition]);
            transaction.commit();
            currentFragmentPosition=position;
        }
    }
}